package city.spring.modules.medicine.controller;

import city.spring.modules.medicine.entity.MedicineQuantityEntity;
import city.spring.modules.medicine.service.MedicineQuantityService;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：药品库存表
 *
 * @author 谢金婷
 * @date 2020-12-28 16:16:29
 */
@RestController
@RequestMapping("/medicine_quantity")
public class MedicineQuantityController extends ApiController {
    private final MedicineQuantityService medicineQuantityService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<MedicineQuantityEntity, ?>> entityOrderFields;

    public MedicineQuantityController(MedicineQuantityService medicineQuantityService) {
        this.medicineQuantityService = medicineQuantityService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(MedicineQuantityEntity::getId);
        entityOrderFields.add(MedicineQuantityEntity::getCode);
        entityOrderFields.add(MedicineQuantityEntity::getMedicineId);
        entityOrderFields.add(MedicineQuantityEntity::getCategory);
        entityOrderFields.add(MedicineQuantityEntity::getType);
        entityOrderFields.add(MedicineQuantityEntity::getSpecifications);
        entityOrderFields.add(MedicineQuantityEntity::getDosageunit);
        entityOrderFields.add(MedicineQuantityEntity::getUnit);
        entityOrderFields.add(MedicineQuantityEntity::getDescription);
        entityOrderFields.add(MedicineQuantityEntity::getUnitprice);
        entityOrderFields.add(MedicineQuantityEntity::getSupplierId);
        entityOrderFields.add(MedicineQuantityEntity::getProductiondate);
        entityOrderFields.add(MedicineQuantityEntity::getExpirationdate);
        entityOrderFields.add(MedicineQuantityEntity::getStorehouseId);
        entityOrderFields.add(MedicineQuantityEntity::getWarehousingquantity);
        entityOrderFields.add(MedicineQuantityEntity::getDeliveryquantity);
        entityOrderFields.add(MedicineQuantityEntity::getQuantity);
        entityOrderFields.add(MedicineQuantityEntity::getWarehousingInventoryAmount);
        entityOrderFields.add(MedicineQuantityEntity::getDeliveryInventoryAmount);
        entityOrderFields.add(MedicineQuantityEntity::getWarehousedate);
        entityOrderFields.add(MedicineQuantityEntity::getUpperWarningvalue);
        entityOrderFields.add(MedicineQuantityEntity::getLowerWarningvalue);
        entityOrderFields.add(MedicineQuantityEntity::getGmtCreate);
        entityOrderFields.add(MedicineQuantityEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>药品库存表</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault Pageable pageable, MedicineQuantityEntity entity) {
        LambdaQueryChainWrapper<MedicineQuantityEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>药品库存表</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault Pageable pageable, MedicineQuantityEntity entity) {
        LambdaQueryChainWrapper<MedicineQuantityEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        Page<MedicineQuantityEntity> page = lambdaQuery.page(MyBatisUtils.toPage(pageable, false));
        return success(page);
    }

    /**
     * 获取一个 <strong>药品库存表</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(medicineQuantityService.getById(id));
    }

    /**
     * 添加一个 <strong>药品库存表</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody MedicineQuantityEntity entity) {
        medicineQuantityService.saveMedicineQuantity(entity);

        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.LOCATION, String.valueOf(entity.getId()));
        return new ResponseEntity<>(headers, HttpStatus.CREATED);
    }

    /**
     * 修改一个 <strong>药品库存表</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody MedicineQuantityEntity entity) {
        entity.setId(id);
        medicineQuantityService.updateMedicineQuantity(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>药品库存表</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        medicineQuantityService.deleteMedicineQuantity(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>药品库存表</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        medicineQuantityService.deleteMedicineQuantity(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<MedicineQuantityEntity> buildLambdaQuery(MedicineQuantityEntity entity) {
        LambdaQueryChainWrapper<MedicineQuantityEntity> lambdaQuery = medicineQuantityService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), MedicineQuantityEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getCode()), MedicineQuantityEntity::getCode, entity.getCode());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getMedicineId()), MedicineQuantityEntity::getMedicineId, entity.getMedicineId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getCategory()), MedicineQuantityEntity::getCategory, entity.getCategory());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), MedicineQuantityEntity::getType, entity.getType());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSpecifications()), MedicineQuantityEntity::getSpecifications, entity.getSpecifications());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDosageunit()), MedicineQuantityEntity::getDosageunit, entity.getDosageunit());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getUnit()), MedicineQuantityEntity::getUnit, entity.getUnit());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getDescription()), MedicineQuantityEntity::getDescription, entity.getDescription());
        lambdaQuery.eq(entity.getUnitprice() != null, MedicineQuantityEntity::getUnitprice, entity.getUnitprice());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSupplierId()), MedicineQuantityEntity::getSupplierId, entity.getSupplierId());
        lambdaQuery.eq(entity.getProductiondate() != null, MedicineQuantityEntity::getProductiondate, entity.getProductiondate());
        lambdaQuery.eq(entity.getExpirationdate() != null, MedicineQuantityEntity::getExpirationdate, entity.getExpirationdate());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getStorehouseId()), MedicineQuantityEntity::getStorehouseId, entity.getStorehouseId());
        lambdaQuery.eq(entity.getWarehousingquantity() != null, MedicineQuantityEntity::getWarehousingquantity, entity.getWarehousingquantity());
        lambdaQuery.eq(entity.getDeliveryquantity() != null, MedicineQuantityEntity::getDeliveryquantity, entity.getDeliveryquantity());
        lambdaQuery.eq(entity.getQuantity() != null, MedicineQuantityEntity::getQuantity, entity.getQuantity());
        lambdaQuery.eq(entity.getWarehousingInventoryAmount() != null, MedicineQuantityEntity::getWarehousingInventoryAmount, entity.getWarehousingInventoryAmount());
        lambdaQuery.eq(entity.getDeliveryInventoryAmount() != null, MedicineQuantityEntity::getDeliveryInventoryAmount, entity.getDeliveryInventoryAmount());
        lambdaQuery.eq(entity.getWarehousedate() != null, MedicineQuantityEntity::getWarehousedate, entity.getWarehousedate());
        lambdaQuery.eq(entity.getUpperWarningvalue() != null, MedicineQuantityEntity::getUpperWarningvalue, entity.getUpperWarningvalue());
        lambdaQuery.eq(entity.getLowerWarningvalue() != null, MedicineQuantityEntity::getLowerWarningvalue, entity.getLowerWarningvalue());
        lambdaQuery.eq(entity.getGmtCreate() != null, MedicineQuantityEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, MedicineQuantityEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<MedicineQuantityEntity> buildQuery(MedicineQuantityEntity entity) {
        QueryWrapper<MedicineQuantityEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getCode()), "code", entity.getCode());
        wrapper.like(StringUtils.isNotBlank(entity.getMedicineId()), "medicine_id", entity.getMedicineId());
        wrapper.like(StringUtils.isNotBlank(entity.getCategory()), "category", entity.getCategory());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "type", entity.getType());
        wrapper.like(StringUtils.isNotBlank(entity.getSpecifications()), "specifications", entity.getSpecifications());
        wrapper.like(StringUtils.isNotBlank(entity.getDosageunit()), "dosageunit", entity.getDosageunit());
        wrapper.like(StringUtils.isNotBlank(entity.getUnit()), "unit", entity.getUnit());
        wrapper.like(StringUtils.isNotBlank(entity.getDescription()), "description", entity.getDescription());
        wrapper.eq(entity.getUnitprice() != null, "unitprice", entity.getUnitprice());
        wrapper.like(StringUtils.isNotBlank(entity.getSupplierId()), "supplier_id", entity.getSupplierId());
        wrapper.eq(entity.getProductiondate() != null, "productiondate", entity.getProductiondate());
        wrapper.eq(entity.getExpirationdate() != null, "expirationdate", entity.getExpirationdate());
        wrapper.like(StringUtils.isNotBlank(entity.getStorehouseId()), "storehouse_id", entity.getStorehouseId());
        wrapper.eq(entity.getWarehousingquantity() != null, "warehousingquantity", entity.getWarehousingquantity());
        wrapper.eq(entity.getDeliveryquantity() != null, "deliveryquantity", entity.getDeliveryquantity());
        wrapper.eq(entity.getQuantity() != null, "quantity", entity.getQuantity());
        wrapper.eq(entity.getWarehousingInventoryAmount() != null, "warehousing_inventory_amount", entity.getWarehousingInventoryAmount());
        wrapper.eq(entity.getDeliveryInventoryAmount() != null, "delivery_inventory_amount", entity.getDeliveryInventoryAmount());
        wrapper.eq(entity.getWarehousedate() != null, "warehousedate", entity.getWarehousedate());
        wrapper.eq(entity.getUpperWarningvalue() != null, "upper_warningvalue", entity.getUpperWarningvalue());
        wrapper.eq(entity.getLowerWarningvalue() != null, "lower_warningvalue", entity.getLowerWarningvalue());
        wrapper.eq(entity.getGmtCreate() != null, "gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
